﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DemoGame1
{
    public class Guard : Enemy
    {

        Playscreen playscreen;
        int type;
        private int damage = 10;
        public Guard(ContentManager _content, int type, Vector2 _topleft, Vector2 _size, Playscreen _pl)
        {
            this.type = type;
            content = _content;
            playscreen = _pl;
            if (type == 1)
            {
                standSequence = CreateTexture("Terry Bogard\\stand\\Terry_", 12);
                walkSequence = CreateTexture("Terry Bogard\\walk\\Terry_", 6);
                attackSequence = CreateTexture("Terry Bogard\\attack\\Terry_", 85);
                // attackSequence = CreateTexture("Terry Bogard\\attack\\Terry_", 5);
                damagedSequence = CreateTexture("Terry Bogard\\damaged\\Terry_", 8);
                deadSequence = CreateTexture("Terry Bogard\\dead\\Terry_", 6);
                InitEnemy(_topleft, _size);
                SetOrigin(new Vector2(290, 282));
                SetTransform(SpriteEffects.FlipHorizontally);
            }
            if (type == 2)
            {
                standSequence = CreateTexture("Haohmaru\\stand\\Haohmaru_", 20);
                walkSequence = CreateTexture("Haohmaru\\walk\\Haohmaru_", 6);
                attackSequence = CreateTexture("Haohmaru\\attack\\Haohmaru_", 74);
                // attackSequence = CreateTexture("Haohmaru\\attack\\Haohmaru_", 4);
                damagedSequence = CreateTexture("Haohmaru\\damaged\\Haohmaru_", 8);
                deadSequence = CreateTexture("Haohmaru\\dead\\Haohmaru_", 6);
                InitEnemy(_topleft, _size);
                SetOrigin(new Vector2(290, 282));
                SetTransform(SpriteEffects.FlipHorizontally);
            }
            HP = 100;
            this.SetScale(1.5f);

        }


        public override void Update(GameTime gameTime)
        {
            Player player = playscreen.player;
            if (dead == 0)
            {
                if (Status == DAMAGED && !player.ball.Collidewith(this) && player.Status != ATK)
                {
                    Status = STAND;
                    SetFrameSequence(STAND);
                }

                /*       if (player.Status == DAMAGED && player.Status!=at)
                       {
                           playscreen.player.Status = STAND;
                       }*/
                if (Collidewith(player))
                {
                    if (player.Status == ATK)
                    {
                        HP -= 10;
                        if (Status != DAMAGED)
                        {
                            Status = DAMAGED;
                            SetFrameSequence(DAMAGED);
                            myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.damaged);
                        }
                    }
                    else
                    {
                        if (Status != ATK)
                        {
                            Status = ATK;
                            SetFrameSequence(ATK);

                            //terry
                            if (type == 1)
                            {
                                myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.punch);
                            }
                            //kiem
                            if (type == 2)
                            {
                                myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.sword);
                            }
                        }
                        if (player.TopLeft.X < this.TopLeft.X)
                        {
                            SetTransform(SpriteEffects.None);
                        }
                        else
                        {
                            SetTransform(SpriteEffects.FlipHorizontally);
                        }
                        Global.BroadcastEvent(Global.EVENTS.GM_PLAYER_DAMAGED, this, this.damage);
                        //player.Damaged(this.damage);

                    }

                }
                if (player.ball.Collidewith(this))
                {
                    HP -= 20;
                    player.ball.Collide = true;
                    if (Status != DAMAGED)
                    {
                        Status = DAMAGED;
                        SetFrameSequence(DAMAGED);
                    }
                }
                else
                {
                    player.ball.Collide = false;
                }
                if (!player.ball.Collidewith(this) && !Collidewith(player))
                {
                    Automove();
                }

                if (HP <= 0)
                {
                    if (Status != DEAD)
                    {
                        Status = DEAD;
                        SetFrameSequence(DEAD);
                    }
                    dead = 1;
                    return;

                }
            }
            if (dead > 0)
            {
                dead++;
                if (Sprites[0].iTextures + 1 == Sprites[0].nTextures)
                {
                    bUpdate = false;
                    // Visible = false;
                }
                if (dead == 15)
                {
                    Visible = false;
                }
                if (dead < 10)
                {
                    myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.dead);
                }
            }

            BoundRect = new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)_Size.X, (int)_Size.Y);
            this.SetScale(1.5f);
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {

            base.Draw(spriteBatch, gameTime);
        }


    }
}
